package model;

import java.util.Date;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import databeans.FundHistory;
import databeans.Position;

public class PositionDAO {
    //FactoryBean objects participate in the containing BeanFactory's synchronization of bean creation. 
	private BeanFactory<Position> factory;
	
	public PositionDAO() throws DAOException {
		try {
	        // Get a BeanTable to create the "position" table
	        BeanTable<Position> positionTable = BeanTable.getInstance(Position.class,"cfa_position");
	        
	        if (!positionTable.exists()) {
	            
	            positionTable.create("customerID", "fundID");
	            
	            // Long running web apps need to clean up idle database connections.
	            // So we can tell each BeanTable to clean them up. 
	            positionTable.setIdleConnectionCleanup(true);
	    
	            // Get a BeanFactory which the actions will use to read and write
	            // rows of the "position" table
	            factory = positionTable.getFactory();
	            //initially populate with sample data
	            bootData();
	        } else {
	            
                // Long running web apps need to clean up idle database connections.
                // So we can tell each BeanTable to clean them up. (You would only notice
                // a problem after leaving your web app running for several hours.)
	            positionTable.setIdleConnectionCleanup(true);

                // Get a BeanFactory which the actions will use to read and
                // write rows of the "user" table
                factory = positionTable.getFactory();
	        }
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}
	}

    private void bootData() throws DAOException {
        Position tp1 = new Position();
        tp1.setCustomerID(1);
        tp1.setFundID(1);
        tp1.setShares(891325);
        create(tp1);
        
        Position tp2 = new Position();
        tp2.setCustomerID(1);
        tp2.setFundID(2);
        tp2.setShares(165500);
        create(tp2);
        
        tp2 = new Position();
        tp2.setCustomerID(2);
        tp2.setFundID(3);
        tp2.setShares(34222);
        create(tp2);
        
        tp2 = new Position();
        tp2.setCustomerID(3);
        tp2.setFundID(3);
        tp2.setShares(619400);
        create(tp2);
        
        
        
    }

    
	public void create(Position position) throws DAOException {
		try {
			Transaction.begin();
			int customerID = position.getCustomerID();
			int fundID = position.getFundID(); 
			long shares = position.getShares(); 
        	
			Position dbPosition = factory.create(customerID, fundID);
			dbPosition.setShares(shares); 
			
			factory.copyInto(position,dbPosition);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}

	public void delete(int customerID, int fundID) throws DAOException {
		try {
			Transaction.begin();
    		Position position = factory.lookup(customerID, fundID);

    		if (position == null) {
				throw new DAOException("Position does not exist: customerID = "+customerID+" fundID: "+ fundID);
    		}

    		if (customerID!=position.getCustomerID()) {
				throw new DAOException("Position not found by customerID "+customerID);
    		}

			factory.delete(customerID, fundID);
			Transaction.commit();
		} catch (RollbackException e) {
			throw new DAOException(e);
		} finally {
			if (Transaction.isActive()) Transaction.rollback();
		}
	}
	
	public Position[] getPositions(int customerID) throws DAOException {
		try {
			Position[] list = factory.match(MatchArg.equals("customerID",customerID));
			return list;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
	public Position getPosition(int customerID, int fundID) throws DAOException {
		try {
			Position position = factory.lookup(customerID, fundID);
			return position;
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
	
    public void update(Position p) throws DAOException {
        try {
            Transaction.begin();
            
            int custID = p.getCustomerID();
            int fundID = p.getFundID();
            Position dbPosition = factory.lookup(custID, fundID);
        
            if (dbPosition == null) {
                throw new DAOException("Position does not exist fundid= " + fundID + " custID=" + custID);
            }
            
            dbPosition.setShares(p.getShares());
            Transaction.commit();
        } catch (RollbackException e) {
            throw new DAOException(e);
        } finally {
            if (Transaction.isActive())
                Transaction.rollback();
        }
    }
	
	public Position lookup(int customerID, int fundID) throws DAOException {
		try {
			return factory.lookup(customerID, fundID);
		} catch (RollbackException e) {
			throw new DAOException(e);
		}
	}
}
      
